# Virtual Router Simulator 
#
# Copyright (C) 2009-2010 VRSim Project
# Author: Choonho Son <choonho@kt.com>
# URL: <http://vrsim.googlecode.com>
# For license information, see LICENSE.TXT
#

import VRSim.Globals as globals
from PyQt4 import QtGui,  QtCore
from VRSim.Ui.Ui_QemuConfig import Ui_QemuConfig

class QemuConfig(QtGui.QDialog,  Ui_QemuConfig):
    def __init__(self,  pnode):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.pnode = pnode
        
        #############
        # intilize contents
        #############
        self.initialize()
        
        self.connect(self.pushButton_chImg,  QtCore.SIGNAL('clicked()'),  self.__chooseImg )        
        self.connect(self.buttonBox,  QtCore.SIGNAL('accepted()'),  self.__save )
        self.connect(self.buttonBox,  QtCore.SIGNAL('rejected()'),  self.close )
    
    def initialize(self):
        # hypervisor type
        count = 0
        for item in self.pnode.type_list:
            self.comboBox_Type.addItem(item)
            if self.pnode.type == item:
                self.comboBox_Type.setCurrentIndex(count)
            count += 1
            
        # hypervisor selection
        hypervisors = globals.GApp.topology.hypervisors
        # [name, type, uri]
        # selected item
        vmName = globals.GApp.topology.nodes[self.pnode.nodeid].vm
        index = 0
        for item in hypervisors:
            type = item[1]
            if type == "KVM":
                self.comboBox_Hypervisor.addItem(item[0])
                if vmName == item[0]:
                    self.comboBox_Hypervisor.setCurrentIndex(index)
                index = index + 1
        
        # hostname (Unseen)
        self.hostname = self.pnode.hostname
        
        # num of CPU
        self.comboBox_CPU.addItem("1")
        self.comboBox_CPU.setCurrentIndex(0)
        
        # memory size
        count = 0
        for item in self.pnode.memory_list:
            self.comboBox_Memory.addItem(item)
            if self.pnode.memory == item:
                self.comboBox_Memory.setCurrentIndex(count)
            count += 1
            
        # clock
        count = 0
        for item in self.pnode.clock_list:
            self.comboBox_TIMEZONE.addItem(item)
            if self.pnode.clock == item:
                self.comboBox_TIMEZONE.setCurrentIndex(count)
            count += 1
            
        # hdd img
        if self.pnode.devices.hdd.source:
            self.label_selectedImage.setText(self.pnode.devices.hdd.source)
        #for item in globals.junos_img_list:
        #    self.comboBox_HDD.addItem(item)
        #    if self.pnode.devices.hdd.source == item:
        #        self.comboBox_HDD.setCurrentIndex(count)
        #    count += 1
            
        # nic
        nic0 = self.pnode.devices.nic0
        if nic0:
            if nic0.type == "socket":
                self.comboBox_NIC0.addItem(nic0.content.model)
                
        nic1 = self.pnode.devices.nic1
        if nic1:
            if nic1.type == "socket":
                self.comboBox_NIC1.addItem(nic1.content.model)
        
        nic2 = self.pnode.devices.nic2
        if nic2:
            if nic2.type == "socket":
                self.comboBox_NIC2.addItem(nic2.content.model)
                    
        
        nic3 = self.pnode.devices.nic3
        if nic3:
            if nic3.type == "socket":
                self.comboBox_NIC3.addItem(nic3.content.model)
                    
     
        nic4 = self.pnode.devices.nic4
        if nic4:
            if nic4.type == "socket":
                self.comboBox_NIC4.addItem(nic4.content.model)
     
        nic5 = self.pnode.devices.nic5
        if nic5:
            if nic5.type == "socket":
                self.comboBox_NIC5.addItem(nic5.content.model)
     
        nic6 = self.pnode.devices.nic6
        if nic6:
            if nic6.type == "socket":
                self.comboBox_NIC6.addItem(nic6.content.model)
     
        nic7 = self.pnode.devices.nic7
        if nic7:
            if nic7.type == "socket":
                self.comboBox_NIC7.addItem(nic7.content.model)
          
        
    ##################################
    # Signal
    ##################################
    def __chooseImg(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, "Open file",  "/data/testbed")
        self.label_selectedImage.setText(filename)
        
    def __save(self):
        # hypervisor name
        globals.GApp.topology.nodes[self.pnode.nodeid].vm = str( self.comboBox_Hypervisor.currentText() )
        
        # save Domain information
        print "Save"
        pnode = globals.GApp.topology.nodes[self.pnode.nodeid].physnode
        pnode.type = self.comboBox_Type.currentText()
        pnode.name = self.hostname
        pnode.vcpu = self.comboBox_CPU.currentText()
        pnode.memory = self.comboBox_Memory.currentText()
        pnode.currentMemory = pnode.memory
        pnode.clock = self.comboBox_TIMEZONE.currentText()
        if self.checkBox_PAE.isChecked():
            pnode.features.pae = True
        if self.checkBox_ACPI.isChecked():
            pnode.features.acpi = True
        if self.checkBox_APIC.isChecked():
            pnode.features.apic = True
        
        # HDD
        pnode.devices.hdd.source = self.label_selectedImage.text()
        
        # NIC
        pnode.devices.nic0.content.type = self.comboBox_NIC0.currentText()
        pnode.devices.nic1.content.type = self.comboBox_NIC1.currentText()
        pnode.devices.nic2.content.type = self.comboBox_NIC2.currentText()
        pnode.devices.nic3.content.type = self.comboBox_NIC3.currentText()
        pnode.devices.nic4.content.type = self.comboBox_NIC4.currentText()
        pnode.devices.nic5.content.type = self.comboBox_NIC5.currentText()
        pnode.devices.nic6.content.type = self.comboBox_NIC6.currentText()
        pnode.devices.nic7.content.type = self.comboBox_NIC7.currentText()
        
        #print pnode.getXML()
        
        self.close()
        

        
        
       
        
        
        

      
